﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace QXBook.Controls.ServerControls
{
    public partial class CalendarItem : CompositeControl, INamingContainer
    {
        public event MonthChange MonthChanged;

        protected void MonthChangeEvent(EventArgs e)
        {
            if (MonthChanged != null)
                MonthChanged(this, e);
        }

        private Button btnPreviousYear = new Button();
        private Button btnPerviousMonth = new Button();
        private Button btnNextMonth = new Button();
        private Button btnNextYear = new Button();
        private TextBox litYear = new TextBox();

        //public DateTime CurrentDate
        //{
        //    get { return Convert.ToDateTime(litYear.Text); }
        //    set { litYear.Text = value.ToString("yyyy-MM"); }
        //}

        public DateTime CurrentDate
        {
            get
            {
                return ViewState["vs_CurrentDate"] == null ? DateTime.Now : (DateTime)ViewState["vs_CurrentDate"];
            }
            set
            {
                ViewState["vs_CurrentDate"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //if (!this.Page.IsPostBack)
            //{
                litYear.Text = CurrentDate.ToString("yyyy-MM");
            //}
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            btnPreviousYear.Text = "<<";
            btnPreviousYear.CausesValidation = false;
            btnPerviousMonth.Text = "<";
            btnPerviousMonth.CausesValidation = false;
            btnPreviousYear.Click += new EventHandler(btnPreviousYear_Click);
            btnPerviousMonth.Click += new EventHandler(btnPerviousMonth_Click);
            btnNextYear.Click += new EventHandler(btnNextYear_Click);
            btnNextMonth.Click += new EventHandler(btnNextMonth_Click);
            btnNextYear.Text = ">>";
            btnNextYear.CausesValidation = false;
            btnNextMonth.Text = ">";
            btnNextMonth.CausesValidation = false;
            btnNextYear.CssClass = "calendar-btn";
            btnNextMonth.CssClass = "calendar-btn";
            btnPreviousYear.CssClass = "calendar-btn";
            btnPerviousMonth.CssClass = "calendar-btn";
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Clear();
            this.Controls.Add(btnNextMonth);
            this.Controls.Add(btnNextYear);
            this.Controls.Add(btnPerviousMonth);
            this.Controls.Add(btnPreviousYear);
        }

        protected void GetDate(int monthNum)
        {
            DateTime _date = DateTime.Parse(litYear.Text).AddMonths(monthNum);
            litYear.Text = _date.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
            CurrentDate = DateTime.Parse(litYear.Text);
            //ControlDateBind();
        }

        protected void btnPerviousMonth_Click(object sender, EventArgs e)
        {
            GetDate(-1);
            MonthChangeEvent(e);
        }

        protected void btnPreviousYear_Click(object sender, EventArgs e)
        {
            GetDate(-12);
            MonthChangeEvent(e);
        }

        protected void btnNextYear_Click(object sender, EventArgs e)
        {
            GetDate(12);
            MonthChangeEvent(e);
        }

        protected void btnNextMonth_Click(object sender, EventArgs e)
        {
            GetDate(1);
            MonthChangeEvent(e);
        }

        public void TextChanged_Click(object sender, EventArgs e)
        {
            //ControlDateBind();
            MonthChangeEvent(e);
        }

        //void ControlDateBind()
        //{
        //    btnNextMonth.Enabled = true;
        //    btnNextYear.Enabled = true;
        //    btnPerviousMonth.Enabled = true;
        //    btnPreviousYear.Enabled = true;
        //    DateTime startDate = DateTime.Parse(ddlYear.Items[0].Value);
        //    DateTime endDate = DateTime.Parse(ddlYear.Items[ddlYear.Items.Count - 1].Value);
        //    DateTime selectDate = DateTime.Parse(ddlYear.SelectedValue);
        //    if (selectDate.AddMonths(1) > endDate)
        //    {
        //        btnNextMonth.Enabled = false;
        //    }
        //    if (selectDate.AddMonths(-1) < startDate)
        //    {
        //        btnPerviousMonth.Enabled = false;
        //    }
        //    if (selectDate.AddMonths(12) > endDate)
        //    {
        //        btnNextYear.Enabled = false;
        //    }
        //    if (selectDate.AddMonths(-12) < startDate)
        //    {
        //        btnPreviousYear.Enabled = false;
        //    }
        //}


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //base.Render(writer);
            writer.Write("<div align=\"center\" class=\"magB-5\">");
            btnPreviousYear.RenderControl(writer);
            writer.Write("&nbsp;");
            btnPerviousMonth.RenderControl(writer);
            writer.Write("&nbsp;");
            litYear.RenderControl(writer);
            writer.Write("&nbsp;");
            btnNextMonth.RenderControl(writer);
            writer.Write("&nbsp;");
            btnNextYear.RenderControl(writer);
            writer.Write("</div>");
            /*         <div align="center" class="magB-5">
              <cc2:BaseButton ID="btnPreviousYear" runat="server" CssClass="btn"
                  Text="&lt;&lt;" onclick="btnPreviousYear_Click" />
              <cc2:BaseButton ID="btnPerviousMonth" runat="server" CssClass="btn"
                  Text="&lt;" onclick="btnPerviousMonth_Click" />
               <uc1:DropDownList ID="ddlYear"  ListType="CalendarDate" runat="server"
                  AutoPostBack="True" OnSelectedIndexChanged="TextChanged_Click" />
               <cc2:BaseButton ID="btnNextMonth" runat="server" CssClass="btn"
                  Text="&gt;" onclick="btnNextMonth_Click" />
              <cc2:BaseButton ID="btnNextYear" runat="server" CssClass="btn"
                  Text="&gt;&gt;" onclick="btnNextYear_Click" />
          </div>
           
      <cc1:DataCalendar id="cal1" runat="server" width="100%"            
          DayNameFormat="Full" NextPrevFormat="ShortMonth"  
  SelectionMode="Day" DayField="CalendarDate"
           CellPadding="0"
              ShowGridLines="True" BorderWidth="0px" ShowTitle="False">
              <TodayDayStyle BackColor="Yellow" />
              <OtherMonthDayStyle CssClass="calendar-other" Height="0px" />
              <DayStyle Font-Bold="False" CssClass="calendar-item" Height="50px"
                  VerticalAlign="Top" />
              <DayHeaderStyle CssClass="calendar-day" />
             <ItemTemplate>
                  <div align="left"><a href='CalendarView.aspx?Calendarid=<%# Container.DataItem["Calendarid"].ToString()%>&Date=<%# Container.DataItem["CalendarDate"] %>'><%# Container.DataItem["Title"] %></a>
                    </div>      
              </ItemTemplate>
          </cc1:DataCalendar>
      </div>*/
        }
    }
}
